import {
  txList,
  getTxCount
} from '../../config/apiServe/new-rpc'
import {
  getTokenMsg
} from '../../config/apiServe/fetch-config'
import {
  getTradeSymbol
} from '../../utils/filters'
export default {
  data() {
    return {
      para: require('../../assets/img/computer/paracoin.png'),
      base: require(`../../assets/img/coin/${process.env.VUE_APP_BASECOIN_URL}`),
      Loading: false,
      total: 0,
      showTotal: 0,
      params: [{
        page: {
          number: 1,
          size: 20
        },
        sort: [{
          "key": "height_index",
          "ascending": false
        }]
      }],
      txList: []
    }
  },
  computed: {
    maxHeight() {
      return this.$store.state.maxHeight || 0;
    }
  },
  watch: {
    maxHeight() {
      this.getTxList()
    }
  },
  mounted() {
    this.getTxCount()
    this.getTxList()
  },
  methods: {
    async getTxCount() {
      getTxCount([]).then(res => {
        if (res.error === null) {
          this.total = res.result > 100000 ? 100000 : res.result
          this.showTotal = res.result
        }
      })
    },
    async getTxList() {
      this.Loading = true
      this.txList = []
      const res = await txList(this.params)
      if (res.error === null) {
        res.result.forEach(item => {
          item.symbol = getTradeSymbol(item)
        })
        this.searchIcon(res.result)
      } else {
        this.$notify.warning({
          title: this.$t('components.tip'),
          message: this.$t('components.errRequest')
        })
      }
    },
    searchIcon(trades) {
      let base = this.$store.state.baseCoinIcon
      let promises = trades.map(item => {
        if (item.symbol === this.$store.state.baseCoin) return base
        else if (item.symbol === 'para') return this.para
        else {
          return getTokenMsg(item.symbol).then(res => {
            if (res.code === 0) return res.data[0].icon === '' ? this.base : res.data[0].icon
          }).catch(() => {
            return this.base
          })
        }
      })
      Promise.all(promises).then(res => {
        trades.map((item, index) => {
          item.icon = res[index]
        })
        this.txList = this.checkGroup(trades)
        console.log(this.txList)
        this.Loading = false
      })
    },
    // 交易组模块
    groupCellName(val) {
      switch (val.row.tradeG) {
        case 0:
          break;
        case 1:
          return 'tx-middle'
        case 2:
          return 'tx-end'
        case 3:
          return 'tx-start'
        default:
          break;
      }
    },
    //区分交易组
    checkGroup(arr) {
      arr.map((current, index) => {
        if (current.group_count > 1) {
          current.tradeG = 1 //0-非交易组，1-交易组，3-交易组末条, 2-交易组首条
          // current.tradeG = (current.tx.fee === 0 && arr[index-1].tx.fee!==0)? 2 : 1
          if (current.fee !== 0) {
            current.tradeG = 2
          } else {
            if (index < arr.length) {
              console.log(index)
              current.tradeG = arr[index].next === '' ? 3 : 1
            } else {
              current.tradeG = 3
            }
          }
        } else {
          current.tradeG = 0
        }
      })
      return arr
    },
    pageChange(page) {
      console.log(page);
      this.params[0].page.number = page;
      this.getTxList()
    },
    sizeChange(size) {
      this.params[0].page.size = size;
      this.params[0].page.number = 1;
      this.getTxList()
    }
  }
}